/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.herenit.service.impl;

import com.herenit.model.User;
import com.herenit.service.UserService;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author Administrator
 */
@Stateless
public class UserServiceImpl implements UserService {

    private EntityManager em;

    public UserServiceImpl() {
    }

    public UserServiceImpl(EntityManager em) {
        this.em = em;
    }

    @Override
    public User findByName(String name) {
        Query query = em.createQuery("from User u where u.name=:name");
        query.setParameter("name", name);
        List<User> users = query.getResultList();
        if (users.isEmpty()) {
            return null;
        } else {
            return users.get(0);
        }
    }

    @Override
    public void save(User u) {
        em.getTransaction().begin();
        em.persist(u);
        em.getTransaction().commit();
    }
}
